package com.g7.dsp.util.smart;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.g7.dsp.forprotocol.beans.LiquidLevel;

public class LiquidResolveUtil {

	private static int itemCount = 8;
	private static int hasCheckBitCount = 9;
	public static String imei;

	public static byte[] data;
	public static List<LiquidLevel> dataList = null;

	public static List<LiquidLevel> getDataList() {
		if (dataList == null || dataList.isEmpty()) {
			String txt = new String(data);
			int len = txt.length();

			dataList = new ArrayList<LiquidLevel>();
			if (txt.charAt(0) == '*' && txt.charAt(len - 1) == '#') {
				long curTime = System.currentTimeMillis();
				Date time = new Date(curTime / 1000 * 1000);
				String[] rows = txt.split("#");

				LiquidLevel liquidLevel = new LiquidLevel();
				for (int n = 0; n < rows.length; n++) {
					if (n > 0) {
						// 一次可能上次多条数据，处理数据的时间重复
						time.setTime(time.getTime() + 1000);
					}

					String row = rows[n];
					String[] items = row.split(",");
					int count = items.length;
					if (!(count == itemCount || count == hasCheckBitCount)) {
						System.out.println("Invalid data format: " + row);
						continue;
					}
					if (count == hasCheckBitCount) {
						byte[] datatemp = row.getBytes();
						byte flag = datatemp[0];
						for (int j = 1, c = datatemp.length - 3; j < c; j++)
							flag ^= datatemp[j];

						// 计算获得的校验值 result
						String result = Integer.toHexString(flag & 0xFF);
						if (result.length() == 1) {
							result = '0' + result;
						}
						// 原始的校验值 checkbit
						String checkbit = null;
						if (items[8] != null && items.length > 2) {
							checkbit = items[8].substring(0, 2);
						} else {
							System.out.println("超声波液位计 无效校验位：");
							continue;
						}
						// 校验位无效
						if (result != null && !result.equals(checkbit)) {
							System.out.println("超声波液位计 无效校验位：");
							continue;
						}
					}

					try {

						if (StringUtils.isBlank(liquidLevel.getImei()))
							liquidLevel.setImei(imei);
						if (liquidLevel.getTime() == null)
							liquidLevel.setTime(time);

						if ("*XD2".equals(items[0])) {
							// 液位值
							double ll = Double.parseDouble(items[3]);
							liquidLevel.setSubLevel(ll / 100);
							// 实时液位值
							double rll = Double.parseDouble(items[6]);
							liquidLevel.setRealSubLevel(rll / 100);
						} else {
							double ll = Double.parseDouble(items[3]);
							liquidLevel.setMainLevel(ll / 100);
							// 实时液位值
							double rll = Double.parseDouble(items[6]);
							liquidLevel.setRealMainLevel(rll / 100);
						}

						// 协议, 软/硬件版本号
						if (StringUtils.isBlank(liquidLevel.getVersion()))
							liquidLevel.setVersion(items[1]);

						// 信号强度, 软件状态代码, 硬件故障代码
						String signal = items[4];
						if (liquidLevel.getSignalIntensity() == 0)
							liquidLevel.setSignalIntensity(Integer.parseInt(signal.substring(0, 2), 16));
						if (liquidLevel.getSoftwareStatus() == 0)
							liquidLevel.setSoftwareStatus(Integer.parseInt(signal.substring(2, 3), 36));
						if (liquidLevel.getHardwareStatus() == 0)
							liquidLevel.setHardwareStatus(Integer.parseInt(signal.substring(3, 4), 16));

						String temperature = items[7];
						if (StringUtils.isNumeric(temperature)) {
							liquidLevel.setTemperature(Double.parseDouble(temperature));
						}
					} catch (Exception e) {
						System.out.println("Parse liquid level error: {}" + row + e);
					}
				}
				dataList.add(liquidLevel);
			}
		}
		return dataList;
	}

	public static byte[] str2bytes(String str) {
		if (StringUtils.isEmpty(str)) {
			return null;
		}
		String[] ss = str.split(" ");
		byte[] bytes = null;
		if (ss.length == 1) {
			bytes = new byte[str.length() / 2];
			for (int i = 0; i < bytes.length; i++)
				bytes[i] = (byte) Integer.parseInt(str.substring(i * 2, i * 2 + 2), 16);
		} else {
			bytes = new byte[ss.length];
			for (int i = 0; i < bytes.length; i++){
				bytes[i] = (byte) Integer.parseInt(ss[i], 16);
			}
		}
		return bytes;
	}
	
	public byte[] getData() {
		return data;
	}

	public static void setData(byte[] datainput) {
		data = datainput;
	}
	
//	public static void main(String[] args){
//		String message1 = "2A 58 44 31 2C 34 31 33 47 2C 30 30 2C 33 35 32 33 2C 30 36 31 30 2C 30 30 30 30 2C 33 35 32 33 2C 6E 75 6C 6C 2C 34 36 23 2A 58 44 32 2C 34 31 33 47 2C 30 30 2C 33 36 39 34 2C 30 37 34 30 2C 30 30 30 30 2C 33 36 39 34 2C 6E 75 6C 6C 2C 34 31 23";
//		String message2 = "2A 58 44 31 2C 34 31 33 47 2C 30 30 2C 33 35 30 38 2C 30 37 30 30 2C 30 30 30 30 2C 33 35 30 38 2C 6E 75 6C 6C 2C 34 36 23 2A 58 44 32 2C 34 31 33 47 2C 30 30 2C 35 36 35 34 2C 30 37 34 30 2C 30 30 30 30 2C 35 36 35 34 2C 6E 75 6C 6C 2C 34 31 23";
//		String message3 = "2A 58 44 31 2C 34 31 33 47 2C 30 31 2C 33 35 30 35 2C 30 35 30 30 2C 39 41 30 34 2C 33 35 30 35 2C 6E 75 6C 6C 2C 33 39 23 2A 58 44 32 2C 34 31 33 47 2C 30 31 2C 36 36 34 34 2C 30 36 34 30 2C 43 37 30 33 2C 36 36 34 34 2C 6E 75 6C 6C 2C 33 36 23";
//		String messageinput = "";
//		System.out.println(message1.length());
//	}
}
